package cn.chenxiejia;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/***
 * @program: maven
 * @ClassName: Qr1
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/24 10:07
 * @version : V1.0
 */
public class Qr1 {
    public static void main(String[] args) throws WriterException, IOException, ChecksumException, NotFoundException, FormatException {
        //Map存入编码格式
        Map<EncodeHintType, Object> hint = new HashMap<>();
        //设置二维码内容
        //var a = new BufferedReader(new FileReader("E:\\maven\\aa1.txt"));
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //使用组件设置内容大小格式
        var bm = new QRCodeWriter().encode("http://img.netbian.com/file/2022/0129/004050xGDF7.jpg", BarcodeFormat.QR_CODE, 200, 200, hint);
        var i = MatrixToImageWriter.toBufferedImage(bm);
        ImageIO.write(i, "png", new FileOutputStream("qr.png"));
        //读取QRcode
        var img = ImageIO.read(new File("qr.png"));
        var src = new BufferedImageLuminanceSource(img);
        var binarizer = new HybridBinarizer(src);
        var binaryBitmap = new BinaryBitmap(binarizer);
        var result = new QRCodeReader().decode(binaryBitmap);
        System.out.println(result.getText());
    }

}

